package com.wzc.hc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzc.hc.entity.po.Member;
import com.wzc.hc.entity.po.Owner;
import com.wzc.hc.entity.resp.Result;
import com.wzc.hc.mapper.MemberMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Member")
@Slf4j
public class MemberController {

    @Autowired
    private MemberMapper memberMapper;

    // 添加成员
    @PostMapping("/addMember")
    public Result<Integer> addMember(@RequestBody Member member) {
        try {
            log.info("Adding member: {}", member);
            member.setMemberFounder(null); // 手动置空 memberFounder 字段
            memberMapper.insert(member);
            log.info("Member added successfully");
            return Result.succeed("添加成功");
        } catch (Exception e) {
            return Result.fail();
        }
    }

    //（选择业主）分页查询
    @PostMapping("/getMemberByOwnerId")
    public Result< IPage<Member>> getMemberByOwnerId(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Page<Member> page = new Page<>(pageNum, pageSize);
            IPage<Member> pageResult = memberMapper.selectPage(page, null);
            return Result.ok(pageResult);
        } catch (Exception e) {
            return Result.fail();
        }
    }
    //根据id查询（选择业主）
    @GetMapping("/getMemberById")
    public Result<Member> getMemberById(@RequestParam("id") Long id) {
        try {
            Member member = memberMapper.selectById(id);
            if (member != null) {
                return Result.ok(member);
            } else {
                return Result.failed("成员信息未找到");
            }
        } catch (Exception e) {
            return Result.fail();
        }
    }
   // 根据楼栋编号查询（选择业主）




    //修改成员
    @PutMapping("/updateMember")
    public Result<Integer> updateMember(@RequestBody Member member) {
        try {
            log.info("Updating member: {}", member);
            memberMapper.updateById(member);
            log.info("Member updated successfully");
            return Result.succeed("修改成功");
        } catch (Exception e) {
            return Result.fail();
        }
    }
    //删除成员
    @DeleteMapping("/deleteMember/{id}")
    public Result<Integer> deleteMember(@PathVariable("id") Long id) {
        try {
            log.info("Deleting member with ID: {}", id);
            memberMapper.deleteById(id);
            log.info("Member deleted successfully");
            return Result.succeed("删除成功");
        } catch (Exception e) {
            return Result.fail();
        }
    }

}


